/**
 * 产品接口 
 */
var express = require('express');
var router = express.Router();

var sql = require('../db/index');

/**
 * @swagger
 *       /cloumn/Update:
 *       post:
 *           description: 栏目新增
 *           tags: [栏目管理]
 *           produces:
 *               - application/x-www-form-urlencoded
 *           parameters:
 *              
 *               - name: name,
 *                 description: 名称
 *                 required: true
 *                 type: string
 *               - name: icon,
 *                 description: 图标
 *                 required: false
 *                 type: string
 *               - name: num,
 *                 description: 排序号
 *                 required: false
 *                 type: number
 *              
 *              
 *           responses:
 *               200:
 *                   description: 操作成功
 *               500:
 *                   description: 服务器错误
 */

// 新增或者修改接口  req.body
router.post('/Update', async (req, res) => {
  // 获取前端传递的参数 
  const {  name, icon, num } = req.body;
  // 如果id存在 修改操作 否则就是新增 
  let mysql = '';
  if (id) {
    mysql = `update jz_product_category set name = '${name}',icon = '${icon}',parent_id ='${num}',
      where id=${id}
    `
  } else {
    mysql = `insert into jz_product_category (name,icon, num )
     values('${name}','${icon}',${num})`
  }
  await sql.query(mysql);
  // 给前端做出响应
  res.send({
    status: 200,
    message: "操作成功",
    data: null,
    timestamp: new Date().getTime()
  })
})

/**
 * @swagger
 *      /cloumn/Delete:
 *      post:
 *          description: 删除栏目
 *          tags: [栏目管理]
 *          parameters:
 *              - in: query
 *                name: id
 *                description: 栏目编号
 *                required: true
 *                schema:
 *                  type: number
 *          responses:
 *             '200':
 *                description: 返回栏目列表
 */

// id 是为了知道那个产品被修改状态  
router.post('/deleteById', async (req, res) => {
  // 获取前端传递的参数 
  let { id } = req.body;
  
  await sql.query(`delete from jz_product_category where id = ${id}`);
  // 给前端做出响应
  res.send({
    status: 200,
    message: '删除成功',
    data: null,
    timestamp: new Date().getTime()
  })

})

module.exports = router;